package com.neptune.imapp

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navigation
import com.neptune.imapp.ui.compose.navigation.RouterConfig
import com.neptune.imapp.ui.compose.screen.frontPage.FrontPageViewModel
import com.neptune.imapp.ui.compose.screen.frontPage.help.Help
import com.neptune.imapp.ui.compose.screen.frontPage.login.Login
import com.neptune.imapp.ui.compose.screen.frontPage.register.Register
import com.neptune.imapp.ui.compose.screen.mainPage.Main
import com.neptune.imapp.ui.compose.screen.mainPage.MainViewModel
import com.neptune.imapp.ui.theme.AppTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AppTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    NavigationHost()
                }
            }
        }
    }
}

@SuppressLint("UnrememberedGetBackStackEntry")
@Composable
fun NavigationHost() {
    val navController = rememberNavController()
    NavHost(
        navController = navController,
        startDestination = RouterConfig.ROUTE_FrontPage
    ) {
        //首页
        loginGraph(navController)
        //主页
        composable(RouterConfig.ROUTE_Main) {
            val viewModel = hiltViewModel<MainViewModel>(
                navController.getBackStackEntry(RouterConfig.ROUTE_Main)
            )
            Main(
                viewModel = viewModel, navController = navController,
            )
        }
    }
}

@SuppressLint("UnrememberedGetBackStackEntry")
fun NavGraphBuilder.loginGraph(navController: NavController) {
    navigation(
        startDestination = RouterConfig.ROUTE_Login,
        route = RouterConfig.ROUTE_FrontPage
    ) {
        composable(RouterConfig.ROUTE_Login) {
            val frontViewModel =
                hiltViewModel<FrontPageViewModel>(
                    navController.getBackStackEntry(RouterConfig.ROUTE_FrontPage)
                )
            Login(frontViewModel, navController)
        }

        composable(RouterConfig.ROUTE_Help) {
            val frontViewModel =
                hiltViewModel<FrontPageViewModel>(
                    navController.getBackStackEntry(RouterConfig.ROUTE_FrontPage)
                )
            Help(frontViewModel, navController)
        }

        composable(RouterConfig.ROUTE_Register) {
            val frontViewModel =
                hiltViewModel<FrontPageViewModel>(
                    navController.getBackStackEntry(RouterConfig.ROUTE_FrontPage)
                )
            Register(viewModel = frontViewModel, navController = navController)
        }
    }
}